<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
  >
    <el-form ref="formAdd" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="form.username"
          size="mini"
          class="input-one"
          clearable
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="form.password"
          :disabled="!isAdd"
          size="mini"
          class="input-one"
          clearable
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="form.nickName"
          size="mini"
          class="input-one"
          clearable
          placeholder="请输入昵称"
        ></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="roleId">
        <mk-select-role class="input-one" v-model="form.roleId" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-switch
          v-model="form.state"
          active-color="#13ce66"
          active-text="正常"
          inactive-text="禁用"
        >
        </el-switch>
      </el-form-item>
      <el-divider />
      <mk-get-button @save="save" @close="handleClose" />
    </el-form>
  </el-dialog>
</template>

<script>
import MkGetButton from "@/components/common/mk-get-button";
import MkBackList from "@/components/common/mk-back-list";

import { dirUsersCreate, dirUsersUpdate } from "@/api/modules/sys.users.api";
import MkSelectRole from "@/components/common/mk-select-role";

export default {
  name: "get-users-info",
  components: { MkSelectRole, MkBackList, MkGetButton },
  props: {
    updateData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      dialogTitle: "",
      type: null,
      dialogVisible: false,
      commonKey: 0,
      form: {
        state: true,
        roleId: "",
      },
      isAdd: true,
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],

        password: [{ required: true, message: "请输入密码", trigger: "blur" }],

        nickName: [{ required: true, message: "请输入昵称", trigger: "blur" }],

        roleId: [{ required: true, message: "请选择角色", trigger: "blur" }],

        state: [{ required: true, message: "请选择状态", trigger: "blur" }],
      },
    };
  },
  methods: {
    open(type, typeName, data) {
      this.resetForm();
      this.type = type;
      this.dialogTitle = typeName;
      this.dialogVisible = true;
      if (data) {
        this.form = data;
      }
    },
    save() {
      this.$refs["formAdd"].validate((valid) => {
        if (valid) {
          let p = {};
          this.type == "create"
            ? (p = this.form)
            : (p = {
                username: this.form.username,
                nickName: this.form.nickName,
                roleId: this.form.roleId,
                state: this.form.state,
                id: this.form.id,
                createdAt: this.form.createdAt,
                updatedAt: this.form.updatedAt,
              });
          let _FUC = "";
          _FUC = this.type == "create" ? dirUsersCreate : dirUsersUpdate;
          _FUC(p)
            .then((res) => {
              this.$toast.success(
                this.type == "create" ? "添加成功！" : "修改成功！"
              );
              this.dialogVisible = false;
              this.$emit("success");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.dialogVisible = false;
        })
        .catch((_) => {});
    },
    resetForm() {
      this.form = {
        state: true,
        roleId: "",
      };
    },
  },
};
</script>

<style lang="scss" scoped></style>
